package day021.tcp.demo3;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
    private static final Logger LOGGER = LoggerFactory.getLogger("TcpServer服务端");

    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(9090);
        LOGGER.trace("服务器已启动...");

        Socket server = ss.accept();
        InputStream netInput = server.getInputStream();

        FileOutputStream fos = new FileOutputStream("D:\\ideaProjects\\javase_code\\study-practice\\" +
                "src\\day021\\tcp\\upload\\123.jpg");
        byte[] buf = new byte[1024];
        int len;
        while ((len = netInput.read(buf)) != -1) {
            fos.write(buf, 0, len);
        }

        LOGGER.trace("上传文件接收完毕！");

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
        bw.write("文件上传成功！");
        bw.newLine();

        bw.close();
        fos.close();
        netInput.close();
        server.close();
    }
}
